import {
  siteReserve,
  changeReserve
} from '../../api/index'

Page({

  /* 页面的初始数据 */
  data: {
    id: '',
    siteId: '', //订单id
    date: '',
    list: [],
    listb: [],
    idList: [],
    show: false,
    name: wx.getStorageSync('venueName'), //场馆名称
    ydtel: '',
    ydname: '',
    des: '',
    cqid: '',
    xmid: '',
    title: '',
  },
  deleteReserve(e) {
    let index = e.currentTarget.dataset.id
    let listb = this.data.listb
    let idList = this.data.idList
    listb.splice(index, 1)
    idList.splice(index, 1)
    this.setData({
      listb,
      idList
    })
  },
  addCd() {
    let ghcd = true
    wx.navigateTo({
      url: `/pages/fieldArea/index?ghcd=${ghcd}&title=${this.data.title}&xmid=${this.data.xmid}&cqid=${this.data.cqid}&siteId=${this.data.siteId}`
    })
  },
  selectList(e) {
    let index = e.currentTarget.dataset.index
    let list = [...this.data.list]
    list.forEach((s, m) => {
      if (m === index) {
        if (s.isSelect) {
          s.isSelect = false
        } else {
          s.isSelect = true
        }
      }
    })
    this.setData({
      list
    })
  },
  siteReserve() {
    siteReserve({
      param: {
        siteId: this.data.siteId
      }
    }).then(res => {
      let list = res.record.siteList
      list.forEach((s) => {
        s.isSelect = true
      })
      this.setData({
        list,
        id: res.record.id,
        des: res.record.remarks,
        ydname: res.record.userType == '0' ? res.record.memberName : res.record.userName,
        ydtel: res.record.userType == '0' ? res.record.memberPhone : res.record.userPhone,
        hyid: res.record.memberId,
      })
    })
  },

  // 关闭密码输入弹框
  onClose() {
    this.setData({
      show: false,
      pwd: ''
    });
  },

  // 实时获取密码输入框值
  bindKeyInput(e) {
    this.setData({
      pwd: e.detail.value
    })
  },

  // 获取日期
  getDate() {
    let date = new Date();
    let nnn = date.getFullYear()
    let yyy = date.getMonth() + 1
    let rrr = date.getDate()
    let xq = date.getDay()
    let data = ''
    switch (xq) {
      case 1:
        data = '周一'
        break;
      case 2:
        data = '周二'
        break;
      case 3:
        data = '周三'
        break;
      case 4:
        data = '周四'
        break;
      case 5:
        data = '周五'
        break;
      case 6:
        data = '周六'
        break;
      case 0:
        data = '周日'
        break;
    }
    let res = nnn + '年' + yyy + '月' + rrr + '日' + ' ' + data
    this.setData({
      date: res
    })
  },
  // 提交订单
  submit() {
    let arr = this.data.list
    let list = []
    let siteIds = []
    arr.forEach((s) => {
      if (s.isSelect) {
        list.push(s)
      } else {
        siteIds.push(s.id)
      }
    })
    if (list.length === 0) return wx.showToast({
      title: '请选择需要更换场地',
      icon: 'none',
      duration: 2000
    })
    if (this.data.idList.length != list.length) return wx.showToast({
      title: '更换场地应与原场地数量保持一致',
      icon: 'none',
      duration: 2000
    })
  
    changeReserve({
      param: {
        reserveAndOrderId: this.data.id,
        siteIds:[...siteIds,...this.data.idList]
      }
    }).then(res => {
      if (res.errorCode == '000000') {
        wx.showToast({
          title: '换场成功',
          icon: 'success',
          duration: 2000,
          mask: true,
          success(res) {
            setTimeout(() => {
              wx.navigateBack({
                delta: 1,
              })
            }, 2000)
          }
        })
      }
    })
  },
  onChange(e) {
    this.setData({
      hklx: e.detail
    });
  },

  /* 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    this.setData({
      siteId: options.siteId,
      cqid: options.cqid,
      xmid: options.xmid,
      title: options.title
    })
    if (options.idList) {
      this.setData({
        listb: JSON.parse(options.list),
        idList: JSON.parse(options.idList)
      })
    }
    this.getDate()
    this.siteReserve()
  },

  /* 生命周期函数--监听页面初次渲染完成 */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      name: wx.getStorageSync('venueName')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})